package com.vpen.block.queue;

import com.vpen.block.model.PlanDTO;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @version: 1.0
 * @author: Vpen
 * @date: 2021/11/21 15:06
 * @content:
 */
@Slf4j
public class PlanBuff {
    private static final Integer ListSize = 10;
    private final BlockingQueue<PlanDTO> plans = new LinkedBlockingQueue<>();
    private final List<PlanDTO> list = new ArrayList<>(ListSize);

    public void savePlan(PlanDTO plan) throws InterruptedException {
        log.info("saving a plan");
        plans.put(plan);
        log.info("saving success");
    }

    public synchronized void consumerPlan() throws InterruptedException {
        while (plans.size() > ListSize) {
            log.info("阻塞队列：{}",plans);
            for (int i = ListSize; i > 0; i--) {
                list.add(plans.take());
            }
            log.info("{}:save to db:{}", Thread.currentThread().getName(), list);
            list.clear();
        }
    }

}
